/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   09.05.2010
 * @version SVN: $Id: Recipe.java 138 2010-05-15 11:57:52Z christian-kerl@web.de $
 */
package org.cakis.server.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.*;

import org.hibernate.validator.NotNull;

/**
 * Recipe
 *
 * @author Christian Kerl
 */
@Entity
@EntityListeners(TimestampableListener.class)
public class Recipe implements Serializable, Timestampable {
    private static final long serialVersionUID = 1L;

    private Long id;
    private Integer version;

    private UUID uuid;
    private String name;
    private Integer numberOfPersons;
    private String preparationDescription;
    private List<RecipeIngredient> ingredients = new ArrayList<RecipeIngredient>();

    private Date createdAt;
    private Date updatedAt;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Version
    public Integer getVersion() {
        return version;
    }

    @SuppressWarnings("unused")
    private void setVersion(Integer version) {
        this.version = version;
    }

    @Transient
    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        if(this.uuid == null) {
            this.uuid = uuid;
        }
    }

    @NotNull
    @Column(name = "uuid")
    @SuppressWarnings("unused")
    private String getInternalUuid() {
        return uuid != null ? uuid.toString() : null;
    }

    @SuppressWarnings("unused")
    private void setInternalUuid(String uuid) {
        this.uuid = UUID.fromString(uuid);
    }

    @NotNull
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public Integer getNumberOfPersons() {
        return numberOfPersons;
    }

    public void setNumberOfPersons(Integer numberOfPersons) {
        this.numberOfPersons = numberOfPersons;
    }

    @Lob
    public String getPreparationDescription() {
        return preparationDescription;
    }

    public void setPreparationDescription(String preparationDescription) {
        this.preparationDescription = preparationDescription;
    }

    @OneToMany(mappedBy = "recipe", cascade = { CascadeType.ALL })
    public List<RecipeIngredient> getIngredients() {
        return ingredients;
    }

    public void setIngredients(List<RecipeIngredient> ingredients) {
        this.ingredients = ingredients;
    }

    @Temporal(TemporalType.DATE)
    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    @Temporal(TemporalType.DATE)
    public Date getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(Date updatedAt) {
        this.updatedAt = updatedAt;
    }
}
